<?php

define('IN_MOBILE', true);
require '../framework/bootstrap.inc.php';
load()->app('common');
load()->app('template');
require IA_ROOT . '/app/common/bootstrap.app.inc.php';
/**
 * 定时任务
 */
//获取当前小程序id
$sql = 'select uniacid from '.tablename('account_wxapp');
$uniacid = pdo_fetchcolumn($sql);
set_time_limit(0);
$time = time();
//当天开始结束时间
$start = strtotime(date('Y-m-d',$time));
$end = $start + 24*60*60;
$data = array();
$res = pdo_fetch('select id from '.tablename('cjdc_yyystatistics')." where create_time = $start");
if(!empty($res)){
    echo '今天已执行过了';die;
}
//获取当天免费摇次数，人数，以及消费总金额
$type = 1;
//次数
$sql_1 = 'select count(id) from '.tablename('cjdc_yyylog')." where create_time > {$start} and create_time < {$end} and type = {$type}";
$num = pdo_fetchcolumn($sql_1);
//人数
$sql_2 = 'select count(user_id) from '.tablename('cjdc_yyylog')." where create_time > {$start} and create_time < {$end} and type = {$type}".' group by user_id';
$people = pdo_fetchcolumn($sql_2);
//奖励总金额
$sql_3 = 'select sum(points) from '.tablename('cjdc_yyylog')." where create_time > {$start} and create_time < {$end} and type = {$type} and is_use = 1";
$price = pdo_fetchcolumn($sql_3);
$data[] = array(
    'type'=>$type,
    'uniacid'=>$uniacid,
    'use_people'=>$people,
    'use_num'=>$num,
    'award_price'=>$price,
    'cw_people'=>0,
    'pay_price'=>0,
    'create_time'=>$start
);
//一元摇
$type_1 = 2;
//次数
$sql_4 = 'select count(id) from '.tablename('cjdc_yyylog')." where create_time > {$start} and create_time < {$end} and type = {$type_1}";
$num_1 = pdo_fetchcolumn($sql_4);
//人数
$sql_5 = 'select count(user_id) from '.tablename('cjdc_yyylog')." where create_time > {$start} and create_time < {$end} and type = {$type_1}".' group by user_id';
$people_1 = pdo_fetchcolumn($sql_5);
//奖励总金额
$sql_6 = 'select sum(points) from '.tablename('cjdc_yyylog')." where create_time > {$start} and create_time < {$end} and type = {$type_1} and is_use = 1";
$price_1 = pdo_fetchcolumn($sql_6);
//成为会员人数
$sql_7 = 'select count(id) from '.tablename('cjdc_user_pay_log')." where create_time > {$start} and create_time < {$end} and type = 3 and yyy_type = 1";
$cw_people = pdo_fetchcolumn($sql_7);
//成为会员支付金额
$sql_8 = 'select sum(price) from '.tablename('cjdc_user_pay_log')." where create_time > {$start} and create_time < {$end} and type = 3 and yyy_type = 1";
$pay_price = pdo_fetchcolumn($sql_8);
$data[] = array(
    'type'=>$type_1,
    'uniacid'=>$uniacid,
    'use_people'=>$people_1,
    'use_num'=>$num_1,
    'award_price'=>$price_1,
    'cw_people'=>$cw_people,
    'pay_price'=>$pay_price,
    'create_time'=>$start
);
//会员摇 9.9
$type_2 = 4;
//次数
$sql_9 = 'select count(id) from '.tablename('cjdc_yyylog')." where create_time > {$start} and create_time < {$end} and type = {$type_2}";
$num_2 = pdo_fetchcolumn($sql_9);
//人数
$sql_10 = 'select count(user_id) from '.tablename('cjdc_yyylog')." where create_time > {$start} and create_time < {$end} and type = {$type_2}".' group by user_id';
$people_2 = pdo_fetchcolumn($sql_10);
//奖励总金额
$sql_11 = 'select sum(points) from '.tablename('cjdc_yyylog')." where create_time > {$start} and create_time < {$end} and type = {$type_2} and is_use = 1";
$price_2 = pdo_fetchcolumn($sql_11);
//成为会员人数
$sql_12 = 'select count(id) from '.tablename('cjdc_user_pay_log')." where create_time > {$start} and create_time < {$end} and type = 3 and yyy_type = 2";
$cw_people_2 = pdo_fetchcolumn($sql_12);
//成为会员支付金额
$sql_13 = 'select sum(price) from '.tablename('cjdc_user_pay_log')." where create_time > {$start} and create_time < {$end} and type = 3 and yyy_type = 2";
$pay_price_2 = pdo_fetchcolumn($sql_13);
$data[] = array(
    'type'=>$type_2,
    'uniacid'=>$uniacid,
    'use_people'=>$people_2,
    'use_num'=>$num_2,
    'award_price'=>$price_2,
    'cw_people'=>$cw_people_2,
    'pay_price'=>$pay_price_2,
    'create_time'=>$start
);
//会员摇 39
$type_3 = 5;
//次数
$sql_14 = 'select count(id) from '.tablename('cjdc_yyylog')." where create_time > {$start} and create_time < {$end} and type = {$type_3}";
$num_3 = pdo_fetchcolumn($sql_14);
//人数
$sql_15 = 'select count(user_id) from '.tablename('cjdc_yyylog')." where create_time > {$start} and create_time < {$end} and type = {$type_3}".' group by user_id';
$people_3 = pdo_fetchcolumn($sql_15);
//奖励总金额
$sql_16 = 'select sum(points) from '.tablename('cjdc_yyylog')." where create_time > {$start} and create_time < {$end} and type = {$type_3} and is_use = 1";
$price_3 = pdo_fetchcolumn($sql_16);
//成为会员人数
$sql_17 = 'select count(id) from '.tablename('cjdc_user_pay_log')." where create_time > {$start} and create_time < {$end} and type = 3 and yyy_type = 3";
$cw_people_3 = pdo_fetchcolumn($sql_17);
//成为会员支付金额
$sql_18 = 'select sum(price) from '.tablename('cjdc_user_pay_log')." where create_time > {$start} and create_time < {$end} and type = 3 and yyy_type = 3";
$pay_price_3 = pdo_fetchcolumn($sql_18);
$data[] = array(
    'type'=>$type_3,
    'uniacid'=>$uniacid,
    'use_people'=>$people_3,
    'use_num'=>$num_3,
    'award_price'=>$price_3,
    'cw_people'=>$cw_people_3,
    'pay_price'=>$pay_price_3,
    'create_time'=>$start
);
//会员摇 99
$type_5 = 6;
//次数
$sql_14 = 'select count(id) from '.tablename('cjdc_yyylog')." where create_time > {$start} and create_time < {$end} and type = {$type_5}";
$num_5 = pdo_fetchcolumn($sql_14);
//人数
$sql_15 = 'select count(user_id) from '.tablename('cjdc_yyylog')." where create_time > {$start} and create_time < {$end} and type = {$type_5}".' group by user_id';
$people_5 = pdo_fetchcolumn($sql_15);
//奖励总金额
$sql_16 = 'select sum(points) from '.tablename('cjdc_yyylog')." where create_time > {$start} and create_time < {$end} and type = {$type_5} and is_use = 1";
$price_5 = pdo_fetchcolumn($sql_16);
//成为会员人数
$sql_17 = 'select count(id) from '.tablename('cjdc_user_pay_log')." where create_time > {$start} and create_time < {$end} and type = 3 and yyy_type = 4";
$cw_people_5 = pdo_fetchcolumn($sql_17);
//成为会员支付金额
$sql_18 = 'select sum(price) from '.tablename('cjdc_user_pay_log')." where create_time > {$start} and create_time < {$end} and type = 3 and yyy_type = 4";
$pay_price_5 = pdo_fetchcolumn($sql_18);
$data[] = array(
    'type'=>$type_5,
    'uniacid'=>$uniacid,
    'use_people'=>$people_5,
    'use_num'=>$num_5,
    'award_price'=>$price_5,
    'cw_people'=>$cw_people_5,
    'pay_price'=>$pay_price_5,
    'create_time'=>$start
);
//会员摇 总和
$type_4 = 3;
//次数
$num_4 = $num_2+$num_3+$num_5;
//人数
$people_4 = $people_2+$people_3+$people_5;
//奖励总金额
$price_4 = $price_2+$price_3+$price_5;
//成为会员人数
$cw_people_4 = $cw_people_2+$cw_people_3+$cw_people_5;
//成为会员支付金额
$pay_price_4 = $pay_price_2+$pay_price_3+$pay_price_5;
$data[] = array(
    'type'=>$type_4,
    'uniacid'=>$uniacid,
    'use_people'=>$people_4,
    'use_num'=>$num_4,
    'award_price'=>$price_4,
    'cw_people'=>$cw_people_4,
    'pay_price'=>$pay_price_4,
    'create_time'=>$start
);
//添加数据
foreach ($data as $key=>$value){
    pdo_insert('cjdc_yyystatistics',$value);
}
//商家资金收入统计
//查询所有商家
$sql_a = 'select id,md_type from '.tablename('cjdc_store')." where uniacid = {$uniacid} and is_open = 1 and is_rest = 2 order by id asc";
$store = pdo_fetchall($sql_a);
if(!empty($store)){
    foreach ($store as $key=>$value){
        //预约数量
        $sql_b = 'select count(id) from '.tablename('cjdc_ydorder')." where uniacid = {$uniacid} and store_id = {$value['id']} and create_time > {$start} 
        and create_time < {$end} and status = 1";
        $yd_num = pdo_fetchcolumn($sql_b);
        //定金收入
        $sql_c = 'select ifnull(sum(yd_price),0) from '.tablename('cjdc_ydorder')." where uniacid = {$uniacid} and store_id = {$value['id']} and create_time > {$start} 
        and create_time < {$end} and status = 1";
        $yd_price = pdo_fetchcolumn($sql_c);
        //消费收入
        $sql_d = 'select ifnull(sum(xf_price),0) from '.tablename('cjdc_ydorder')." where uniacid = {$uniacid} and store_id = {$value['id']} and com_time > {$start} 
        and com_time < {$end} and status = 2";
        $xf_price = pdo_fetchcolumn($sql_d);
        //总收入
        $total_price = $yd_price + $xf_price;
        $data_a = array(
            'uniacid'=>$uniacid,
            'store_id'=>$value['id'],
            'yd_num'=> $yd_num,
            'yd_price'=>$yd_price,
            'xf_price'=>$xf_price,
            'total_price'=>$total_price,
            'create_time'=>$start
        );
        pdo_insert('cjdc_store_fund_stati',$data_a);
    }
}
//查询商家分类
$sql_e = 'select id from '.tablename('cjdc_storetype')." where uniacid = {$uniacid}";
$storetype = pdo_fetchall($sql_e);
if(!empty($storetype)){
    foreach ($storetype as $key=>$value){
        $sql_f = 'select id from '.tablename('cjdc_store')." where uniacid = {$uniacid}
         and is_open = 1 and is_rest = 2 and md_type = {$value['id']} order by id asc";
        $store_ids = pdo_fetchall($sql_f);
        if(!empty($store_ids)){
            $store_ids_a = array();
            foreach ($store_ids as $v){
                $store_ids_a[] = $v['id'];
            }
            $store_ids = implode(',',$store_ids_a);
            //预约数量
            $sql_b = 'select ifnull(sum(yd_num),0) from '.tablename('cjdc_store_fund_stati')." where uniacid = {$uniacid} and 
            store_id in ({$store_ids}) and type = 1 and create_time = {$start}";
            $yd_num = pdo_fetchcolumn($sql_b);
            //定金收入
            $sql_c = 'select ifnull(sum(yd_price),0) from '.tablename('cjdc_store_fund_stati')." where uniacid = {$uniacid} and 
            store_id in ({$store_ids}) and type = 1 and create_time = {$start}";
            $yd_price = pdo_fetchcolumn($sql_c);
            //消费收入
            $sql_d = 'select ifnull(sum(xf_price),0) from '.tablename('cjdc_store_fund_stati')." where uniacid = {$uniacid} and 
            store_id in ({$store_ids}) and type = 1 and create_time = {$start}";
            $xf_price = pdo_fetchcolumn($sql_d);
            //总收入
            $total_price = $yd_price + $xf_price;
            $data_b = array(
                'uniacid'=>$uniacid,
                'storetype_id'=>$value['id'],
                'yd_num'=> $yd_num,
                'yd_price'=>$yd_price,
                'xf_price'=>$xf_price,
                'total_price'=>$total_price,
                'create_time'=>$start,
                'type'=>2
            );
        }else{
            $data_b = array(
                'uniacid'=>$uniacid,
                'storetype_id'=>$value['id'],
                'yd_num'=> 0,
                'yd_price'=>0,
                'xf_price'=>0,
                'total_price'=>0,
                'create_time'=>$start,
                'type'=>2
            );
        }
        pdo_insert('cjdc_store_fund_stati',$data_b);
    }
}
//商家对账表更或添加
//查询所有商家
$sql_a = 'select id,`name`,md_type from '.tablename('cjdc_store')." where uniacid = {$uniacid} order by id asc";
$store = pdo_fetchall($sql_a);
if(!empty($store)){
    foreach ($store as $key=>$value){
        //定金收入
        $sql_c = 'select ifnull(sum(yd_price),0) from '.tablename('cjdc_ydorder')." where uniacid = {$uniacid} and store_id = {$value['id']}
         and status in (1,2)";
        $yd_price = pdo_fetchcolumn($sql_c);
        //消费收入
        $sql_d = 'select ifnull(sum(xf_price),0) from '.tablename('cjdc_ydorder')." where uniacid = {$uniacid} and store_id = {$value['id']} 
        and status = 2";
        $xf_price = pdo_fetchcolumn($sql_d);
        //总收入
        $total_price = $yd_price + $xf_price;
        //平台利润
        $sql_j = 'select yd_poundage from '.tablename('cjdc_storetype')." where uniacid = {$uniacid} and id = {$value['md_type']}";
        $rate = pdo_fetchcolumn($sql_j);
        $platform_price = $total_price*$rate/100;
        //商家已获得收入
        $sql_k = 'select ifnull(sum(price),0) from '.tablename('cjdc_store_remit')." where uniacid = {$uniacid} and store_id = 
        {$value['id']}";
        $store_getprice = pdo_fetchcolumn($sql_k);
        //商家可获得收入
        $store_price = $total_price - $platform_price - $store_getprice;
        $remit = pdo_get('cjdc_store_reconciliation',array('store_id'=>$value['id']));
        if(empty($remit)){
            $data_a = array(
                'uniacid'=>$uniacid,
                'store_id'=>$value['id'],
                'store_name'=> $value['name'],
                'total_price'=>$total_price,
                'platform_price'=>$platform_price,
                'store_getprice'=>$store_getprice,
                'store_price'=>$store_price,
                'update_time'=>time()
            );
            pdo_insert('cjdc_store_reconciliation',$data_a);
        }else{
            $data_a = array(
                'total_price'=>$total_price,
                'platform_price'=>$platform_price,
                'store_getprice'=>$store_getprice,
                'store_price'=>$store_price,
                'update_time'=>time()
            );
            pdo_update('cjdc_store_reconciliation',$data_a,array('id'=>$remit['id']));
        }
    }
}
//清空商家餐桌类型预定数量
pdo_update('cjdc_table_type',array('yd_num'=>0),array('uniacid'=>$uniacid));
//查询超过8天未完成的订单
$time_a = time() + 8*24*60*60;
$sql_i = 'select id from '.tablename('cjdc_ydorder')." where uniacid = {$uniacid} and status = 1 and create_time > {$time_a}";
$ydorderlist = pdo_fetchall($sql_i);
if(!empty($ydorderlist)){
    foreach ($ydorderlist as $value){
        pdo_update('cjdc_ydorder',array('status'=>3,'com_time'=>time()),array('id'=>$value['id']));
    }
}
echo '执行成功';die;
